package org.example.ymcc.web.controller;

import org.example.ymcc.domain.CourseDoc;
import org.example.ymcc.dto.CourseDocQuery;
import org.example.ymcc.repository.CourseDocElasticsearchRepository;
import org.example.ymcc.repository.EmployeeElasticsearchRepository;
import org.example.ymcc.result.JsonResult;
import org.example.ymcc.service.CourseDocQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/search")
public class EsController {
    @Autowired
    private CourseDocElasticsearchRepository repository;
    @Autowired
    private CourseDocQueryService courseDocQueryService;

    @PostMapping("/batchSave")
    public JsonResult batchSave(@RequestBody List<CourseDoc> docList){
        repository.saveAll(docList);
        return JsonResult.success();
    }

    @PostMapping("/queryCourse")
    public JsonResult queryCourse(@RequestBody CourseDocQuery courseDocQuery){
        return courseDocQueryService.queryCourse(courseDocQuery);
    }

    @GetMapping("/getCourseDoc/{courseId}")
    public JsonResult getCourseDoc(@PathVariable("courseId")Long courseId){
        Optional<CourseDoc> courseDoc = repository.findById(courseId);
        return JsonResult.success().setData(courseDoc.orElseGet(CourseDoc::new));
    }

}
